package at.socialconference.backend.model;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

import org.hibernate.annotations.LazyCollection;
import org.hibernate.annotations.LazyCollectionOption;

@Entity
@Table(name = "t_conferences")
public class Conference implements Insertable {

	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	@Column(name = "conferenceid")
	private Integer id;

	@Column(name = "name")
	private String name;
	@Column(name = "date_from")
	private Date date_from;
	@Column(name = "date_to")
	private Date date_to;
	@Column(name = "description")
	private String description;

	@ManyToOne
	@JoinColumn(name = "place")
	@LazyCollection(LazyCollectionOption.FALSE)
	private Place place;
	
	@ManyToMany
	@JoinTable(name = "t_conference_talk", joinColumns = { @JoinColumn(name = "conferenceid") }, inverseJoinColumns = { @JoinColumn(name = "talkid") })
	@LazyCollection(LazyCollectionOption.FALSE)
	private List<Talk> talks = new ArrayList<Talk>();

	public Conference() {}

	public Conference(at.socialconference.inter.model.Conference conference) {
		this.date_from = conference.getDate_from();
		this.date_to = conference.getDate_to();
		this.description = conference.getDescription();
		this.id = conference.getId();
		this.name = conference.getName();
		for (at.socialconference.inter.model.Talk t : conference.getTalks()) {
			this.talks.add(new Talk(t));
		}
		this.place = new Place(conference.getPlace());
	}




	public Integer getId() {
		return id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Date getDate_from() {
		return date_from;
	}

	public void setDate_from(Date date_from) {
		this.date_from = date_from;
	}

	public Date getDate_to() {
		return date_to;
	}

	public void setDate_to(Date date_to) {
		this.date_to = date_to;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public void setPlace(Place place) {
		this.place = place;
	}

	public Place getPlace() {
		return place;
	}


	public void setTalks(List<Talk> talks) {
		this.talks = talks;
	}

	public List<Talk> getTalks() {
		return talks;
	}

	public static ArrayList<at.socialconference.inter.model.Conference> getInterConferences(List<Conference> confs) {

		ArrayList<at.socialconference.inter.model.Conference> result = new ArrayList<at.socialconference.inter.model.Conference>();
		for (int i = 0; i < confs.size(); i++) {
			at.socialconference.inter.model.Conference temp = confs.get(i).generateInterConference();
			result.add(i, temp);
		}

		return result;
	}
	
	public at.socialconference.inter.model.Conference generateInterConference() {
		List<at.socialconference.inter.model.Talk> tt = new ArrayList<at.socialconference.inter.model.Talk>();
		
		for (Talk t : talks) {
			tt.add(t.generateInterTalk()); // VORLÄUFIG 0
		}

		return new at.socialconference.inter.model.Conference(id, name, date_from, date_to, description, place.generateInterPlace(),tt);
	}
}
